home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / iccfont.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  9.4 KB  |  376 lines

  1. /* Copyright (C) 1992, 1995, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: iccfont.c,v 1.2 2000/09/19 19:00:41 lpd Exp $ */
  20. /* Initialization support for compiled fonts */
  21. #include "string_.h"
  22. #include "ghost.h"
  23. #include "gsstruct.h"        /* for iscan.h */
  24. #include "ccfont.h"
  25. #include "errors.h"
  26. #include "ialloc.h"
  27. #include "idict.h"
  28. #include "ifont.h"
  29. #include "iname.h"
  30. #include "isave.h"        /* for ialloc_ref_array */
  31. #include "iutil.h"
  32. #include "oper.h"
  33. #include "ostack.h"        /* for iscan.h */
  34. #include "store.h"
  35. #include "stream.h"        /* for iscan.h */
  36. #include "strimpl.h"        /* for sfilter.h for picky compilers */
  37. #include "sfilter.h"        /* for iscan.h */
  38. #include "iscan.h"
  39.  
  40. /* ------ Private code ------ */
  41.  
  42. /* Forward references */
  43. private int cfont_ref_from_string(P4(i_ctx_t *, ref *, const char *, uint));
  44.  
  45. typedef struct {
  46.     i_ctx_t *i_ctx_p;
  47.     const char *str_array;
  48.     ref next;
  49. } str_enum;
  50.  
  51. inline private void
  52. init_str_enum(str_enum *pse, i_ctx_t *i_ctx_p, const char *ksa)
  53. {
  54.     pse->i_ctx_p = i_ctx_p;
  55.     pse->str_array = ksa;
  56. }
  57.  
  58. typedef struct {
  59.     cfont_dict_keys keys;
  60.     str_enum strings;
  61. } key_enum;
  62.  
  63. inline private void
  64. init_key_enum(key_enum *pke, i_ctx_t *i_ctx_p, const cfont_dict_keys *pkeys,
  65.           const char *ksa)
  66. {
  67.     pke->keys = *pkeys;
  68.     init_str_enum(&pke->strings, i_ctx_p, ksa);
  69. }
  70.  
  71. /* Check for reaching the end of the keys. */
  72. inline private bool
  73. more_keys(const key_enum *pke)
  74. {
  75.     return (pke->keys.num_enc_keys | pke->keys.num_str_keys);
  76. }
  77.  
  78. /* Get the next string from a string array. */
  79. /* Return 1 if it was a string, 0 if it was something else, */
  80. /* or an error code. */
  81. private int
  82. cfont_next_string(str_enum * pse)
  83. {
  84.     const byte *str = (const byte *)pse->str_array;
  85.     uint len = (str[0] << 8) + str[1];
  86.  
  87.     if (len == 0xffff) {
  88.     make_null(&pse->next);
  89.     pse->str_array += 2;
  90.     return 0;
  91.     } else if (len >= 0xff00) {
  92.     int code;
  93.  
  94.     len = ((len & 0xff) << 8) + str[2];
  95.     code = cfont_ref_from_string(pse->i_ctx_p, &pse->next,
  96.                      pse->str_array + 3, len);
  97.     if (code < 0)
  98.         return code;
  99.     pse->str_array += 3 + len;
  100.     return 0;
  101.     }
  102.     make_const_string(&pse->next, avm_foreign, len, str + 2);
  103.     pse->str_array += 2 + len;
  104.     return 1;
  105. }
  106.  
  107. /* Put the next entry into a dictionary. */
  108. /* We know that more_keys(kp) is true. */
  109. private int
  110. cfont_put_next(ref * pdict, key_enum * kep, const ref * pvalue)
  111. {
  112.     i_ctx_t *i_ctx_p = kep->strings.i_ctx_p;
  113.     cfont_dict_keys * const kp = &kep->keys;
  114.     ref kname;
  115.     int code;
  116.  
  117.     if (pdict->value.pdict == 0) {
  118.     /* First time, create the dictionary. */
  119.     code = dict_create(kp->num_enc_keys + kp->num_str_keys +
  120.                kp->extra_slots, pdict);
  121.     if (code < 0)
  122.         return code;
  123.     }
  124.     if (kp->num_enc_keys) {
  125.     const charindex *skp = kp->enc_keys++;
  126.  
  127.     code = array_get(®istered_Encoding(skp->encx), (long)(skp->charx),
  128.              &kname);
  129.     kp->num_enc_keys--;
  130.     } else {            /* must have kp->num_str_keys != 0 */
  131.     code = cfont_next_string(&kep->strings);
  132.     if (code != 1)
  133.         return (code < 0 ? code : gs_note_error(e_Fatal));
  134.     code = name_ref(kep->strings.next.value.const_bytes,
  135.             r_size(&kep->strings.next), &kname, 0);
  136.     kp->num_str_keys--;
  137.     }
  138.     if (code < 0)
  139.     return code;
  140.     return dict_put(pdict, &kname, pvalue, &i_ctx_p->dict_stack);
  141. }
  142.  
  143. /* ------ Routines called from compiled font initialization ------ */
  144.  
  145. /* Create a dictionary with general ref values. */
  146. private int
  147. cfont_ref_dict_create(i_ctx_t *i_ctx_p, ref *pdict,
  148.               const cfont_dict_keys *kp, cfont_string_array ksa,
  149.               const ref *values)
  150. {
  151.     key_enum kenum;
  152.     const ref *vp = values;
  153.  
  154.     init_key_enum(&kenum, i_ctx_p, kp, ksa);
  155.     pdict->value.pdict = 0;
  156.     while (more_keys(&kenum)) {
  157.     const ref *pvalue = vp++;
  158.     int code = cfont_put_next(pdict, &kenum, pvalue);
  159.  
  160.     if (code < 0)
  161.         return code;
  162.     }
  163.     return 0;
  164. }
  165.  
  166. /* Create a dictionary with string/null values. */
  167. private int
  168. cfont_string_dict_create(i_ctx_t *i_ctx_p, ref *pdict,
  169.              const cfont_dict_keys *kp, cfont_string_array ksa,
  170.              cfont_string_array kva)
  171. {
  172.     key_enum kenum;
  173.     str_enum senum;
  174.     uint attrs = kp->value_attrs;
  175.  
  176.     init_key_enum(&kenum, i_ctx_p, kp, ksa);
  177.     init_str_enum(&senum, i_ctx_p, kva);
  178.     pdict->value.pdict = 0;
  179.     while (more_keys(&kenum)) {
  180.     int code = cfont_next_string(&senum);
  181.  
  182.     switch (code) {
  183.         default:        /* error */
  184.         return code;
  185.         case 1:        /* string */
  186.         r_set_attrs(&senum.next, attrs);
  187.         case 0:        /* other */
  188.         ;
  189.     }
  190.     code = cfont_put_next(pdict, &kenum, &senum.next);
  191.     if (code < 0)
  192.         return code;
  193.     }
  194.     return 0;
  195. }
  196.  
  197. /* Create a dictionary with number values. */
  198. private int
  199. cfont_num_dict_create(i_ctx_t *i_ctx_p, ref * pdict,
  200.               const cfont_dict_keys * kp, cfont_string_array ksa,
  201.               const ref * values, const char *lengths)
  202. {
  203.     key_enum kenum;
  204.     const ref *vp = values;
  205.     const char *lp = lengths;
  206.     ref vnum;
  207.  
  208.     init_key_enum(&kenum, i_ctx_p, kp, ksa);
  209.     pdict->value.pdict = 0;
  210.     while (more_keys(&kenum)) {
  211.     int len = (lp == 0 ? 0 : *lp++);
  212.     int code;
  213.  
  214.     if (len == 0)
  215.         vnum = *vp++;
  216.     else {
  217.         --len;
  218.         make_const_array(&vnum, avm_foreign | a_readonly, len, vp);
  219.         vp += len;
  220.     }
  221.     code = cfont_put_next(pdict, &kenum, &vnum);
  222.     if (code < 0)
  223.         return code;
  224.     }
  225.     return 0;
  226. }
  227.  
  228. /* Create an array with name values. */
  229. private int
  230. cfont_name_array_create(i_ctx_t *i_ctx_p, ref * parray, cfont_string_array ksa,
  231.             int size)
  232. {
  233.     int code = ialloc_ref_array(parray, a_readonly, size,
  234.                 "cfont_name_array_create");
  235.     ref *aptr = parray->value.refs;
  236.     int i;
  237.     str_enum senum;
  238.  
  239.     if (code < 0)
  240.     return code;
  241.     init_str_enum(&senum, i_ctx_p, ksa);
  242.     for (i = 0; i < size; i++, aptr++) {
  243.     ref nref;
  244.     int code = cfont_next_string(&senum);
  245.  
  246.     if (code != 1)
  247.         return (code < 0 ? code : gs_note_error(e_Fatal));
  248.     code = name_ref(senum.next.value.const_bytes,
  249.             r_size(&senum.next), &nref, 0);
  250.     if (code < 0)
  251.         return code;
  252.     ref_assign_new(aptr, &nref);
  253.     }
  254.     return 0;
  255. }
  256.  
  257. /* Create an array with string/null values. */
  258. private int
  259. cfont_string_array_create(i_ctx_t *i_ctx_p, ref * parray,
  260.               cfont_string_array ksa, int size, uint attrs)
  261. {
  262.     int code = ialloc_ref_array(parray, a_readonly, size,
  263.                 "cfont_string_array_create");
  264.     ref *aptr = parray->value.refs;
  265.     int i;
  266.     str_enum senum;
  267.  
  268.     if (code < 0)
  269.     return code;
  270.     init_str_enum(&senum, i_ctx_p, ksa);
  271.     for (i = 0; i < size; i++, aptr++) {
  272.     int code = cfont_next_string(&senum);
  273.  
  274.     switch (code) {
  275.         default:        /* error */
  276.         return code;
  277.         case 1:        /* string */
  278.         r_set_attrs(&senum.next, attrs);
  279.         case 0:        /* other */
  280.         ;
  281.     }
  282.     ref_mark_new(&senum.next);
  283.     *aptr = senum.next;
  284.     }
  285.     return 0;
  286. }
  287.  
  288. /* Create an array with scalar values. */
  289. private int
  290. cfont_scalar_array_create(i_ctx_t *i_ctx_p, ref * parray,
  291.               const ref *va, int size, uint attrs)
  292. {
  293.     int code = ialloc_ref_array(parray, attrs, size,
  294.                 "cfont_scalar_array_create");
  295.     ref *aptr = parray->value.refs;
  296.     uint elt_attrs = attrs | ialloc_new_mask;
  297.     int i;
  298.  
  299.     if (code < 0)
  300.     return code;
  301.     memcpy(aptr, va, size * sizeof(ref));
  302.     for (i = 0; i < size; i++, aptr++)
  303.     r_set_attrs(aptr, elt_attrs);
  304.     return 0;
  305. }
  306.  
  307. /* Create a name. */
  308. private int
  309. cfont_name_create(i_ctx_t *i_ctx_p, ref * pnref, const char *str)
  310. {
  311.     return name_ref((const byte *)str, strlen(str), pnref, 0);
  312. }
  313.  
  314. /* Create an object by parsing a string. */
  315. private int
  316. cfont_ref_from_string(i_ctx_t *i_ctx_p, ref * pref, const char *str, uint len)
  317. {
  318.     scanner_state sstate;
  319.     stream s;
  320.     int code;
  321.  
  322.     scanner_state_init(&sstate, false);
  323.     sread_string(&s, (const byte *)str, len);
  324.     code = scan_token(i_ctx_p, &s, pref, &sstate);
  325.     return (code <= 0 ? code : gs_note_error(e_Fatal));
  326. }
  327.  
  328. /* ------ Initialization ------ */
  329.  
  330. /* Procedure vector passed to font initialization procedures. */
  331. private const cfont_procs ccfont_procs = {
  332.     cfont_ref_dict_create,
  333.     cfont_string_dict_create,
  334.     cfont_num_dict_create,
  335.     cfont_name_array_create,
  336.     cfont_string_array_create,
  337.     cfont_scalar_array_create,
  338.     cfont_name_create,
  339.     cfont_ref_from_string
  340. };
  341.  
  342. /* null    .getccfont    <number-of-fonts> */
  343. /* <int>   .getccfont    <font-object> */
  344. private int
  345. zgetccfont(i_ctx_t *i_ctx_p)
  346. {
  347.     os_ptr op = osp;
  348.     int code;
  349.     const ccfont_fproc *fprocs;
  350.     int nfonts;
  351.     int index;
  352.  
  353.     code = ccfont_fprocs(&nfonts, &fprocs);
  354.     if (code != ccfont_version)
  355.     return_error(e_invalidfont);
  356.  
  357.     if (r_has_type(op, t_null)) {
  358.     make_int(op, nfonts);
  359.     return 0;
  360.     }
  361.     check_type(*op, t_integer);
  362.     index = op->value.intval;
  363.     if (index < 0 || index >= nfonts)
  364.     return_error(e_rangecheck);
  365.  
  366.     return (*fprocs[index]) (i_ctx_p, &ccfont_procs, op);
  367. }
  368.  
  369. /* Operator table initialization */
  370.  
  371. const op_def ccfonts_op_defs[] =
  372. {
  373.     {"0.getccfont", zgetccfont},
  374.     op_def_end(0)
  375. };
  376.